<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" encoding="gb2312" />
  <xsl:param name="languageCode" />
  <xsl:param name="currencyFormat" />
  <xsl:param name="numberFormat" />
  <xsl:param name="dateFormat" />
  <xsl:param name="dateSeparator" />
  <!--
	if a label does not exist, which language to use as the default
-->
  <xsl:variable name="defaultLanguageCode">1033</xsl:variable>
  <xsl:template match="/entity">
    <entity>
      <xsl:attribute name="name">
        <xsl:value-of select="@name" />
      </xsl:attribute>
      <xsl:attribute name="objecttypecode">
        <xsl:value-of select="@objecttypecode" />
      </xsl:attribute>
      <xsl:attribute name="languageCode">
        <xsl:value-of select="$languageCode" />
      </xsl:attribute>
      <xsl:attribute name="currencyFormat">
        <xsl:value-of select="$currencyFormat" />
      </xsl:attribute>
      <xsl:attribute name="numberFormat">
        <xsl:value-of select="$numberFormat" />
      </xsl:attribute>
      <xsl:attribute name="dateFormat">
        <xsl:value-of select="$dateFormat" />
      </xsl:attribute>
      <xsl:attribute name="dateSeparator">
        <xsl:value-of select="$dateSeparator" />
      </xsl:attribute>
      <div>
        <span class="selectBox" value="" changeHandler="onFieldChange">
          <table cellspacing="0" cellpadding="2" style="display:none;">
            <tr>
              <td val="">
              </td>
            </tr>
            <xsl:for-each select="properties/fields/field">
              <xsl:sort select="displaynames/displayname[@languagecode = $languageCode]/@description" />
              <xsl:variable name="fieldName" select="@name" />
              <xsl:variable name="fieldDataType" select="/entity/metadata/fields/field[@name = $fieldName]/@datatype" />
              <tr>
                <td nowrap="">
                  <xsl:attribute name="val">
                    <xsl:choose>
                      <xsl:when test="options or states or statuses">
                        <xsl:value-of select="$fieldName" />
                        <xsl:text>,picklist</xsl:text>
                      </xsl:when>
                      <xsl:when test="$fieldDataType = 'float' or $fieldDataType = 'integer' or $fieldDataType = 'money'">
                        <xsl:value-of select="$fieldName" />,<xsl:value-of select="$fieldDataType" />,<xsl:value-of select="@minvalue" />,<xsl:value-of select="@maxvalue" />,<xsl:value-of select="@accuracy" /></xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="$fieldName" />,<xsl:value-of select="$fieldDataType" /></xsl:otherwise>
                    </xsl:choose>
                  </xsl:attribute>
                  <xsl:choose>
                    <xsl:when test="displaynames/displayname[@languagecode = $languageCode]">
                      <xsl:value-of select="displaynames/displayname[@languagecode = $languageCode]/@description" />
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="displaynames/displayname[@languagecode = $defaultLanguageCode]/@description" />
                    </xsl:otherwise>
                  </xsl:choose>
                </td>
              </tr>
            </xsl:for-each>
          </table>
        </span>
      </div>
      <fields>
        <xsl:for-each select="properties/fields/field">
          <xsl:sort select="displaynames/displayname[@languagecode = $languageCode]/@description" />
          <xsl:variable name="fieldName" select="@name" />
          <xsl:variable name="fieldDataType" select="/entity/metadata/fields/field[@name = $fieldName]/@datatype" />
          <field>
            <xsl:attribute name="name">
              <xsl:value-of select="$fieldName" />
            </xsl:attribute>
            <xsl:attribute name="isrequired">
              <xsl:value-of select="/entity/metadata/fields/field[@name = $fieldName]/@requiredforplatform" />
            </xsl:attribute>
            <xsl:attribute name="validforcreateapi">
              <xsl:value-of select="/entity/metadata/fields/field[@name = $fieldName]/@validforcreateapi" />
            </xsl:attribute>
            <xsl:attribute name="validforupdateapi">
              <xsl:value-of select="/entity/metadata/fields/field[@name = $fieldName]/@validforupdateapi" />
            </xsl:attribute>
            <xsl:attribute name="datatype">
              <xsl:choose>
                <xsl:when test="options or states or statuses">
                  <xsl:text>picklist</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="/entity/metadata/fields/field[@name = $fieldName]/@datatype" />
                </xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:attribute name="desc">
              <xsl:choose>
                <xsl:when test="displaynames/displayname[@languagecode = $languageCode]">
                  <xsl:value-of select="displaynames/displayname[@languagecode = $languageCode]/@description" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="displaynames/displayname[@languagecode = $defaultLanguageCode]/@description" />
                </xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:choose>
              <xsl:when test="$fieldDataType = 'float' or $fieldDataType = 'integer' or $fieldDataType = 'money'">
                <xsl:attribute name="minvalue">
                  <xsl:value-of select="@minvalue" />
                </xsl:attribute>
                <xsl:attribute name="maxvalue">
                  <xsl:value-of select="@maxvalue" />
                </xsl:attribute>
                <xsl:attribute name="accuracy">
                  <xsl:value-of select="@accuracy" />
                </xsl:attribute>
              </xsl:when>
            </xsl:choose>
          </field>
        </xsl:for-each>
      </fields>
      <values>
        <xsl:for-each select="metadata/fields/field[@datatype = 'lookup']">
          <lookup>
            <xsl:attribute name="name">
              <xsl:value-of select="@name" />
            </xsl:attribute>
            <xsl:attribute name="objects">
              <xsl:value-of select="@objecttypecode" />
            </xsl:attribute>
            <xsl:attribute name="mapto">
              <xsl:value-of select="@name" />
            </xsl:attribute>
          </lookup>
        </xsl:for-each>
        <xsl:for-each select="metadata/fields/field[@datatype = 'customer']">
          <lookup objects="1,2">
            <xsl:attribute name="name">
              <xsl:value-of select="@name" />
            </xsl:attribute>
            <xsl:attribute name="mapto">
              <xsl:value-of select="@name" />
            </xsl:attribute>
          </lookup>
        </xsl:for-each>
        <xsl:for-each select="metadata/fields/field[@datatype = 'owner']">
          <lookup name="ownerid" objects="8" mapto="ownerid">
          </lookup>
        </xsl:for-each>
        <xsl:for-each select="properties/fields/field">
          <xsl:if test="options or states or statuses">
            <div>
              <xsl:attribute name="name">
                <xsl:value-of select="@name" />
              </xsl:attribute>
              <span class="selectBox" changeHandler="onSelectValueChange">
                <xsl:attribute name="value">
                  <xsl:value-of select="options/option[1]/@value" />
                </xsl:attribute>
                <table cellspacing="0" cellpadding="2" style="display:none;">
                  <xsl:for-each select="*/*">
                    <tr>
                      <td nowrap="true">
                        <xsl:attribute name="val">
                          <xsl:value-of select="@value" />
                        </xsl:attribute>
                        <xsl:choose>
                          <xsl:when test="name()='status'">
                            <xsl:variable name="stateValue" select="@state" />
                            <xsl:choose>
                              <xsl:when test="/entity/properties/fields/field/states/state[@value = $stateValue]/labels/label[@languagecode = $languageCode]">
                                <xsl:value-of select="/entity/properties/fields/field/states/state[@value = $stateValue]/labels/label[@languagecode = $languageCode]/@description" />
                              </xsl:when>
                              <xsl:otherwise>
                                <xsl:value-of select="/entity/properties/fields/field/states/state[@value = $stateValue]/labels/label[@languagecode = $defaultLanguageCode]/@description" />
                              </xsl:otherwise>
                            </xsl:choose>
                            <xsl:text> - </xsl:text>
                            <xsl:choose>
                              <xsl:when test="labels/label[@languagecode = $languageCode]">
                                <xsl:value-of select="labels/label[@languagecode = $languageCode]/@description" />
                              </xsl:when>
                              <xsl:otherwise>
                                <xsl:value-of select="labels/label[@languagecode = $defaultLanguageCode]/@description" />
                              </xsl:otherwise>
                            </xsl:choose>
                          </xsl:when>
                          <xsl:otherwise>
                            <xsl:choose>
                              <xsl:when test="labels/label[@languagecode = $languageCode]">
                                <xsl:value-of select="labels/label[@languagecode = $languageCode]/@description" />
                              </xsl:when>
                              <xsl:otherwise>
                                <xsl:value-of select="labels/label[@languagecode = $defaultLanguageCode]/@description" />
                              </xsl:otherwise>
                            </xsl:choose>
                          </xsl:otherwise>
                        </xsl:choose>
                      </td>
                    </tr>
                  </xsl:for-each>
                </table>
              </span>
            </div>
          </xsl:if>
        </xsl:for-each>
      </values>
    </entity>
  </xsl:template>
</xsl:stylesheet>